package com.example.mpandroidchart_all.FragmentChart;

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.mpandroidchart_all.R;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.Utils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class FragmentBarChart extends Fragment {
    private BarChart barChart;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_barchart,container,false);
        barChart=view.findViewById(R.id.bar_chart);

        setXAxis();
        setYAxis();
        setLegend();
        setDescription();
        initData();

        return view;
    }
    private void initData(){
        barChart.setExtraOffsets(24f,48f,24f,24f);
        setChartData();

    }
    private void setChartData(){
        final List<BarEntry> value1=new ArrayList<>();
        BarEntry [] entries={new BarEntry(0f,396),new BarEntry(1f,1089),
                new BarEntry(2f,963),new BarEntry(3f,756),new BarEntry(4f,287)};
        for(int i=0;i<entries.length;i++){
            value1.add(entries[i]);
        }

        List<BarEntry> value2=new ArrayList<>();
        value2.add(new BarEntry(0f,245));
        value2.add(new BarEntry(1f,520));
        value2.add(new BarEntry(2f,504));
        value2.add(new BarEntry(3f,517));
        value2.add(new BarEntry(4f,186));

        BarDataSet set1=new BarDataSet(value1,"有违章");
        set1.setColor(Color.GREEN);
        set1.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                if((int) (value%5)==0){
                    return new DecimalFormat("##.0").format((value/(value+245))*100)+"%";
                }else if((int) (value%5)==1){
                    return new DecimalFormat("##.0").format((value/(value+520))*100)+"%";
                }else if((int) (value%5)==2){
                    return new DecimalFormat("##.0").format((value/(value+504))*100)+"%";
                }else if((int) (value%5)==3){
                    return new DecimalFormat("##.0").format((value/(value+517))*100)+"%";
                }else if((int) (value%5)==4){
                    return new DecimalFormat("##.0").format((value/(value+186))*100)+"%";
                }
                return "";
            }
        });


        BarDataSet set2=new BarDataSet(value2,"无违章");
        set2.setColor(Color.BLUE);
        set2.setDrawValues(false);

        List<IBarDataSet> barDataSets=new ArrayList<>();
        barDataSets.add(set1);
        barDataSets.add(set2);

        BarData barData=new BarData(barDataSets);
        barData.setBarWidth(0.5f);

        barChart.setData(barData);
    }
    private void setXAxis(){
        XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        final String [] strings={"90后","80后","70后","60后","50后"};
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return strings[(int) (value%5)];
            }
        });
        xAxis.setLabelCount(5);
        xAxis.setDrawGridLines(false);
        xAxis.setTextSize(15f);
        xAxis.setTypeface(Typeface.DEFAULT_BOLD); // 加粗字体
    }
    private void setYAxis(){
        YAxis axisRight = barChart.getAxisRight();
        axisRight.setEnabled(false);

        YAxis axisLeft = barChart.getAxisLeft();
        axisLeft.setAxisMinimum(0); // 最小值为0
        axisLeft.setAxisMaximum(1200); // 最大值为1200
        axisLeft.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return (int)value+"";
            }
        });
    }
    private void setLegend(){
        Legend legend = barChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setOrientation(Legend.LegendOrientation.VERTICAL);
        legend.setDrawInside(true);
    }
    private void setDescription(){
        Description description = barChart.getDescription();
        String descriptionStr="年龄群体车辆违章的占比统计";
        description.setText(descriptionStr);
        description.setTextSize(10f);
        description.setTextColor(Color.RED);

        //x轴
//        WindowManager wm= (WindowManager) getSystemService(Context.WINDOW_SERVICE);
//        DisplayMetrics dm=new DisplayMetrics();
//        wm.getDefaultDisplay().getMetrics(dm);
        float x=300;

        //y轴
        Paint paint=new Paint();
        float height= Utils.calcTextHeight(paint,descriptionStr);
        float y=Utils.convertDpToPixel(height+12);
        description.setPosition(x,y);
        description.setTextAlign(Paint.Align.CENTER);
    }
}
